package cn.jx.asc.socket;

import cn.jx.asc.common.untils.StringToTimeUtils;
import cn.jx.asc.user.pojo.Control;
import cn.jx.asc.user.pojo.Humidity;
import cn.jx.asc.user.pojo.Record;
import cn.jx.asc.user.pojo.Valve;
import cn.jx.asc.user.service.UserService;
import cn.jx.asc.user.service.UserServiceImpl;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;
import java.util.Date;

/*
 * 负责处理终端上上传的数据
 *
 */


public class HandleData {
    int flag = -1;
    int length = -1;
    int id = -1;
    JSONObject data = null;
    private UserService userService;
    //创建一个容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-cfg.xml");

    public HandleData(int id, JSONObject da) {
        this.id = id;
        this.data = da;
    }

    public boolean handle() throws JSONException {
        try {
            flag = data.getInt("flags");
        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("***handleData 解析数据错误 analysis data error***");
        }

        //更新终端存活时间

        switch(flag) {
            case 0:
            {
                System.out.println("***获得心跳 get heartbeat***");
                break;
            }
            case 1:
            {
                System.out.println("***获得实时数据 get real time data***");
                JSONObject realTimeData = data.getJSONObject("data");
                //将数据写入数据库中
                //封装一个Record对象
                Record record = new Record();
                record.setRecordTime(new Date());
                record.setTemperature((short) realTimeData.getInt("tem"));  //环境温度
                record.setTemperatureSoil((short) realTimeData.getInt("soilTem"));  //土壤温度
                record.setHumidity((short) realTimeData.getInt("hum")); //环境湿度
                record.setHumiditySoil((short) realTimeData.getInt("soilTem")); //土壤湿度
                record.setCo2((short) realTimeData.getInt("co2"));
                record.setSun(realTimeData.getInt("sun"));
                record.setSensorId(500000041);
                userService = (UserService)applicationContext.getBean("userServiceImpl");
                userService.insertRecord(record);
                break;
            }
            case 2:
            {
                System.out.println("***获得阀门状态 get valve state***");
                JSONArray valveState = data.getJSONArray("data");
                //将数据写入数据库
                /*data":{"vlaveNum":"1","vlaveState":"1"},"ID":"123456"}*/
                for(int i=0;i<valveState.length();i++){
                    JSONObject json = valveState.getJSONObject(i);
                    Valve valve = null;
                    if(json.getString("valveNum").equals("1")){
                        valve = new Valve();
                        valve.setValveId(400000066);
                        valve.setValveState(Short.valueOf(json.getString("valveNum")));
                    }else if(json.getString("valveNum").equals("2")){
                        valve = new Valve();
                        valve.setValveId(400000067);
                        valve.setValveState(Short.valueOf(json.getString("valveNum")));
                    }else if(json.getString("valveNum").equals("3")){
                        valve = new Valve();
                        valve.setValveId(400000068);
                        valve.setValveState(Short.valueOf(json.getString("valveNum")));
                    }else if(json.getString("valveNum").equals("4")){
                        valve = new Valve();
                        valve.setValveId(400000069);
                        valve.setValveState(Short.valueOf(json.getString("valveNum")));
                    }
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.updateValve(valve);
                }
                break;
            }
            case 3:
            {
                System.out.println("***获得定时 get timing***");
                System.out.println(data);
                int valveNum = data.getInt("valveNum");
                String time1 = data.getString("time1");
                String time2 = data.getString("time2");
                String time3 = data.getString("time3");
                Control control = null;
                if(!time1.equals("000000000")){
                    String times1[] = StringToTimeUtils.stringToTime(time1);
                    String timeStart = times1[0];
                    String timeStop = times1[1];
                    String interval = times1[2];
                    control = new Control();
                    control.setStartTime(timeStart);
                    control.setStopTime(timeStop);
                    control.setIntervalDay(interval);
                    control.setDaysRemaining((short) 30);
                    if(valveNum==1){
                        control.setValveId(400000066);
                    }else if(valveNum==2){
                        control.setValveId(400000067);
                    }else if(valveNum==3){
                        control.setValveId(400000068);
                    }else if(valveNum==4){
                        control.setValveId(400000069);
                    }
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.insertControl(control);
                }
                if(!time1.equals("000000000")){
                    String times2[] = StringToTimeUtils.stringToTime(time2);
                    String timeStart = times2[0];
                    String timeStop = times2[1];
                    String interval = times2[2];
                    control = new Control();
                    control.setStartTime(timeStart);
                    control.setStopTime(timeStop);
                    control.setIntervalDay(interval);
                    control.setDaysRemaining((short) 30);
                    if(valveNum==1){
                        control.setValveId(400000066);
                    }else if(valveNum==2){
                        control.setValveId(400000067);
                    }else if(valveNum==3){
                        control.setValveId(400000068);
                    }else if(valveNum==4){
                        control.setValveId(400000069);
                    }
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.insertControl(control);
                }
                if(!time1.equals("000000000")){
                    String times3[] = StringToTimeUtils.stringToTime(time3);String times1[] = StringToTimeUtils.stringToTime(time1);
                    String timeStart = times3[0];
                    String timeStop = times3[1];
                    String interval = times3[2];
                    control = new Control();
                    control.setStartTime(timeStart);
                    control.setStopTime(timeStop);
                    control.setIntervalDay(interval);
                    control.setDaysRemaining((short) 30);
                    if(valveNum==1){
                        control.setValveId(400000066);
                    }else if(valveNum==2){
                        control.setValveId(400000067);
                    }else if(valveNum==3){
                        control.setValveId(400000068);
                    }else if(valveNum==4){
                        control.setValveId(400000069);
                    }
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.insertControl(control);
                }

               /* //将数据写入数据库
                for(int i=0;i<times.length();i++){
                    JSONObject json = times.getJSONObject(i);
                    Control control = null;
                    String timeStart = null;
                    String timeStop = null;
                    if(valveNum==1){
                        control = new Control();
                        timeStart = json.getString("Start_Hour")+":"+json.getString("Start_Minute")+":"+json.getString("Start_Second");
                        timeStop = json.getString("Stop_Hour")+":"+json.getString("Stop_Minute")+":"+json.getString("Stop_Second");
                        control.setStartTime(timeStart);
                        control.setStopTime(timeStop);
                        control.setIntervalDay(json.getString("interval"));
                        control.setDaysRemaining((short) 30);
                        control.setValveId(400000066);
                    }else if(valveNum==2){
                        control = new Control();
                        timeStart = json.getString("Start_Hour")+":"+json.getString("Start_Minute")+":"+json.getString("Start_Second");
                        timeStop = json.getString("Stop_Hour")+":"+json.getString("Stop_Minute")+":"+json.getString("Stop_Second");
                        control.setStartTime(timeStart);
                        control.setStopTime(timeStop);
                        control.setIntervalDay(json.getString("interval"));
                        control.setDaysRemaining((short) 30);
                        control.setValveId(400000067);
                    }else if(valveNum==3){
                        control = new Control();
                        timeStart = json.getString("Start_Hour")+":"+json.getString("Start_Minute")+":"+json.getString("Start_Second");
                        timeStop = json.getString("Stop_Hour")+":"+json.getString("Stop_Minute")+":"+json.getString("Stop_Second");
                        control.setStartTime(timeStart);
                        control.setStopTime(timeStop);
                        control.setIntervalDay(json.getString("interval"));
                        control.setDaysRemaining((short) 30);
                        control.setValveId(400000068);
                    }else if(valveNum==4){
                        control = new Control();
                        timeStart = json.getString("Start_Hour")+":"+json.getString("Start_Minute")+":"+json.getString("Start_Second");
                        timeStop = json.getString("Stop_Hour")+":"+json.getString("Stop_Minute")+":"+json.getString("Stop_Second");
                        control.setStartTime(timeStart);
                        control.setStopTime(timeStop);
                        control.setIntervalDay(json.getString("interval"));
                        control.setDaysRemaining((short) 30);
                        control.setValveId(400000069);
                    }
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.insertControl(control);
                }
                System.out.println(times);*/
                break;
            }
            case 4:
            {
                System.out.println("***获得定湿度 get humidity***");
                System.out.println(data);
                //获得湿度
                String humStr = data.getString("hum");
                Short hum = Short.valueOf(humStr);
                Humidity humidity = new Humidity();
                humidity.setHumidity(hum);
                humidity.setGroupId(300000069);
                //将数据写入数据库
                userService = (UserService)applicationContext.getBean("userServiceImpl");
                userService.insertHumidity(humidity);
                break;
            }
            case 5:
            {
                System.out.println("***获得控制方式改变 get control mode***");
                String valveNumStr = data.getString("valveNum");
                Short valveNum = Short.valueOf(valveNumStr);
                String controlTypeStr = data.getString("data");
                Short controlType = Short.valueOf(controlTypeStr);
                Valve valve = null;
                if(valveNum==1){
                    valve = new Valve();
                    valve.setValveId(400000066);
                    valve.setControlType(controlType);
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.updateValve(valve);
                }else if(valveNum==2){
                    valve = new Valve();
                    valve.setValveId(400000067);
                    valve.setControlType(controlType);
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.updateValve(valve);
                }else if(valveNum==3){
                    valve = new Valve();
                    valve.setValveId(400000068);
                    valve.setControlType(controlType);
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.updateValve(valve);
                }else if(valveNum==4){
                    valve = new Valve();
                    valve.setValveId(400000069);
                    valve.setControlType(controlType);
                    userService = (UserService)applicationContext.getBean("userServiceImpl");
                    userService.updateValve(valve);
                }
                break;
            }
            case 99:
            {
                System.out.println("***确认数据，断开连接 confirm data***");
                System.out.println(data);

                //将数据写入数据库
                break;
            }
            default:
            {
                System.out.println("***数据错误 data error***");
                //将数据写入数据库
                break;

            }
        }

        return true;
    }

}
